import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'

import { useNProgress } from '@vueuse/integrations/useNProgress'
import { dynamicRotuer } from '@/modules/dynamicRotuer'
const { isLoading } = useNProgress()

import '@/assets/scss/nprogress.scss'

let routes: RouteRecordRaw[] = [
  {
    path: '/',
    redirect: '/home/index',
  },
  {
    path: '/login',
    component: () => import('@/view/login/login.vue'),
  },
  {
    path: '/:pathMath(.*)',
    component: () => import('@/view/stateCode/404.vue'),
  },
  {
    name: 'home',
    path: '/home',
    redirect: '/home/index',
    component: () => import('@/layout/index.vue'),
    children: [
      {
        name: 'homeIndex',
        path: '/home/index',
        component: () => import('@/view/home/index.vue'),
      },
    ],
  },
]

const router = createRouter({
  history: createWebHistory(),
  routes,
})

/**
 * 把后台的路由处理成vueRouter所需要的格式
 */
router.beforeEach((to: any, from: any, next: any) => {
  isLoading.value = true
  dynamicRotuer(to, from, next)
})

router.afterEach(() => {
  isLoading.value = false
})
export default router
